<template>
  <div class="department-container">
    <div class="app-container">
      <page-tools title="共有10086条数据">
        <el-button
          v-allow="'import_excel'"
          type="warning"
          size="small"
          @click="$router.push('/import')"
        >excel导入</el-button>
        <el-button
          v-allow="'export_excel'"
          type="danger"
          size="small"
          @click="excelVendor"
        >excel导出</el-button>
        <el-button
          type="primary"
          size="small"
          @click="showDialog = true"
        >新增员工</el-button>
      </page-tools>
    </div>
    <el-card style="margin-top: 10px">
      <el-table border :data="list" :default-sort="{ prop: 'timeOfEntry' }">
        <el-table-column label="序号" type="index" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column prop="workNumber" sortable label="工号" />
        <el-table-column prop="staffPhoto" sortable label="头像">
          <template slot-scope="scope">
            <el-button @click.native="f(scope)" />
            <image-holder :src="scope.row.staffPhoto" />
          </template>
        </el-table-column>
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <template slot-scope="scope">{{
            format(scope.row.formOfEmployment)
          }}</template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="correctionTime" />
        <!-- <el-table-column label="账户状态" /> -->
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="$router.push('/employees/detail?id=' + scope.row.id)"
            >查看</el-button>
            <el-button
              type="text"
              size="small"
              @click="hAssign(scope.row.id)"
            >分配角色</el-button>
            <el-button
              type="text"
              size="small"
              @click="hDel(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          :total="total"
          :current-page="page"
          :page-size="size"
          :page-sizes="[5, 10, 20]"
          layout="total,sizes,prev, pager, next"
          @current-change="hCurrentChange"
          @size-change="hSizeChange"
        />
      </el-row>
    </el-card>
    <el-dialog
      title="新增员工"
      :visible.sync="showDialog"
      @click="showDialog = true"
      @close="hclose"
    >
      <addor-edit
        ref="EmpDialog"
        @close="showDialog = false"
        @success="hsuccess"
      />
    </el-dialog>
    <el-dialog v-if="showDialogRole" :visible.sync="showDialogRole">
      <assignRole :cur-id="curId" @cclose="cclose" />
    </el-dialog>
  </div>
</template>

<script>
import { type_map } from '@/components/constant/index'
import { getEmployee, delEmployee } from '@/api/employees'
import addorEdit from './EmpDialog.vue'
import assignRole from './assignRole.vue'
export default {
  components: { addorEdit, assignRole },
  // created(){
  //  console.log(this.good,this)
  // }
  data() {
    return {
      showDialogRole: false,
      showDialog: false,
      curId: '',
      list: [],
      size: 5, // 一页5条数据
      page: 1, // 当前页码
      total: 0 // 总条数
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    cclose() {
      this.showDialogRole = false
      this.$message.success('修改成功')
    },
    hAssign(id) {
      this.showDialogRole = true
      this.curId = id
    },
    f(scope) {
      console.log(scope, '888')
    },
    formatData(rows) {
      //     const dataArr =
      //  [
      //    ['13600000001', '吕勇锐', '1992-08-04', '正式', '2020-01-01', '0001', '总裁办']
      //      ['13600000002', '袁永安', '1993-08-04', '正式', '2020-01-01', '0002', '总裁办']
      //  ]
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      // const header = Object.keys(rows[0])
      const header = Object.keys(rows[0]).map((item) => map[item])
      const data = rows.map((obj) => {
        obj.formOfEmployment = type_map[obj.formOfEmployment]
        return Object.values[obj]
      })
      return { header, data }
    },
    async excelVendor() {
      const res = await getEmployee({ page: this.page, size: this.size })
      // console.log('12312321', res)
      const rows = res.data.rows
      const { header, data } = this.formatData(rows)
      console.log('222', header, data)
      import('@/vendor/Export2Excel').then((excel) => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header: ['姓名', '工资'], // 表头 必填
          data: [
            ['刘备', 100],
            ['关羽', 500]
          ], // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    hclose() {
      this.$refs.EmpDialog.resetForm()
    },
    hsuccess() {
      this.showDialog = false
      this.total++
      this.page = Math.ceil(this.total / this.size)
      this.loadEmployee()
    },
    async doDel(id) {
      // 1. 调用接口做删除
      await delEmployee(id)
      // 判断如果当前删除是表格中最后一条数据，要把页码-1
      if (this.list.length === 1) {
        if (this.page > 1) {
          this.page--
        }
      }
      // 2. 提示用户，删除成功了
      this.$message.success('删除成功')
      // 3. 重新加载
      this.loadEmployee()
    },
    // 用户点击了删除
    hDel(id) {
      // 弹框确认删除
      // await this.$confirm('确认要删除吗？', '系统提示')
      // this.doDel(id)

      this.$confirm('确认要删除吗？', '系统提示')
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    // 更改每页显示几条数据
    hSizeChange(curPagesize) {
      console.log(curPagesize)
      // 1. 更新参数
      this.size = curPagesize
      this.page = 1
      // 2. 重发请求
      this.loadEmployee()
    },
    // 自动接收当前的页码
    hCurrentChange(curPage) {
      console.log(curPage)
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadEmployee()
    },
    format(type) {
      return type_map[type] || '未知'
    },
    async loadEmployee() {
      const res = await getEmployee({ page: this.page, size: this.size })
      console.log('员工信息', res)
      // 保存
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>

<style></style>
